/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hotelsupplymanagement;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class ProductSelected
{
    JFrame jfps=new JFrame("Enter the Product ID");
    JButton jbps=new JButton("OK");
    JPanel jpps=new JPanel();
    JLabel jl1=new JLabel("Enter the Product ID :");
    JLabel jl2=new JLabel("Enter Quantity");
    JTextField tfproid=new JTextField();;
    JTextField tfproq=new JTextField();;
    FileReader frs1,frs2;
    BufferedReader br1,br2;
    String s,s1,productId,productName,reqInvList;
    String[] cline,ri ;
    String[] reqInv;

    void gettingProduct()
    {
        jfps.setLayout(new BorderLayout());
        tfproid.setColumns(20);
        tfproq.setColumns(20);

        jfps.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        jpps.setSize(35,35);
        jpps.add(jl1);
        jpps.add(tfproid);
        jpps.add(jl2);
        jpps.add(tfproq);
        jpps.add(jbps);

        jbps.addActionListener(new getProductselected());

//      jfps.add(tfproid,BorderLayout.NORTH);
//      jfps.add(jbps,BorderLayout.SOUTH);
        jfps.setSize(300,200);
        jfps.add(jpps,BorderLayout.CENTER);
        jfps.setVisible(true);
        jfps.setLocation(250, 250);
    }


        class getProductselected implements ActionListener{

        public void actionPerformed(ActionEvent event)
        {
            try {
                productSelInvItem();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ProductSelected.class.getName()).log(Level.SEVERE, null, ex);
            }

            }

        }

       /**
        * This Fuction is used for getting the product selected from the user
        * from the Product id given by the user.
        * This function also reads the quantity of the items that are needeed for the User.
        * This function reads from the textfield of the frame and passes the information to the UpdateInv
        *@exception FileNotFoundException This Exception is thrown when the specified file "GUIProduct" is not found in the Location.
        */
        public void productSelInvItem() throws FileNotFoundException {
        try {
            int reqproductid;
            int reqproductqua;
            ArrayList<GetProducts> p = new ArrayList<GetProducts>();
            reqproductid = Integer.parseInt(tfproid.getText());
            reqproductqua = Integer.parseInt(tfproq.getText());

            frs1 = new FileReader("GUIProduct.txt");
            br1 = new BufferedReader(frs1);

            int i = 0;
            while ((s = br1.readLine()) != null)
            {
                p.add(new GetProducts());
                cline = s.split(",");
                p.get(i).setProductName(cline[0]);
                p.get(i).setProductId(Integer.parseInt(cline[1]));
                p.get(i).setProductQuality(Integer.parseInt(cline[2]));
                p.get(i).setProductDescription(cline[3]);
                p.get(i).setProductInvList(cline[4]);
                i++;
            }
                for(int k=0;k<i;k++)
                    {
                        if(p.get(k).getProductId() == reqproductid)
                        {

                            reqInvList=p.get(k).getProductInvList();
                            reqInv=reqInvList.split(";");
                            for(int j=0;j<reqInv.length;j++)
                            {
                                System.out.println("The Inventory Items for the product are : "+reqInv[j]);
                                updateInv(reqInv[j],reqproductqua);
                            }

                        }
                    }

        } catch (IOException ex) {
            Logger.getLogger(ProductSelected.class.getName()).log(Level.SEVERE, null, ex);
        }

        }

        /**
         * This Fuction gets the Inventory list from the method productSelInvItems and 
         * processes that information to update the inventory Items in the text file.
         * @param string This has the string value of the inventory Item that User is selected from the menu
         * @param reqproductqua This has the data of Quantity of the product is selected by the user
         * @exception FileNotFoundException This Exception is thrown when the specified file is not found in the Location.
         * @exception IOException This Exception is thrown when the data does not match exactly with the get Operators
        */
        public void updateInv(String string, int reqproductqua) throws FileNotFoundException, IOException
        {
            ArrayList<GetInventories> q=new ArrayList<GetInventories>();
            int i = 0,lines=0,quantityItem;
            String lineString;
            String[] sline;


            FileOutputStream fos=new FileOutputStream("GUIItem.txt",true);
            PrintStream ps=new PrintStream(fos);
            frs2=new FileReader("GUIItem.txt");
            br2=new BufferedReader(frs2);

            try
                {
                while((s1=br2.readLine()) != null)
                    {
                        q.add(new GetInventories());
                        sline=s1.split(",");
                        q.get(i).setInventoryName(sline[0]);
                        q.get(i).setInventoryId(Integer.parseInt(sline[1]));
                        q.get(i).setInventoryQuantity(Integer.parseInt(sline[2]));
                        q.get(i).setInventoryPrice(Integer.parseInt(sline[3]));
                        i++;
                    }
                for(int k=0;k<i;k++)
                    {
                        if(q.get(k).getInventoryName().equals(string))
                        {

                            quantityItem=q.get(k).getInventoryQuantity();
                            quantityItem=quantityItem-reqproductqua;
                            q.get(k).setInventoryQuantity(quantityItem);
                            for(int j=0;j<i;j++)
                            {

                            ps.println(q.get(j).getInventoryName()+","+q.get(j).getInventoryId()+","+q.get(j).getInventoryQuantity()+","+q.get(j).getInventoryPrice());
                            }
                        }


                    }
                //ps.println(q.get(0).getName()+","+q.get(0).getInventoryid()+","+q.get(0).getQuantity()+","+q.get(0).getPrice());
            }
            catch(Exception ex)
		{
			System.out.println("Error in UpdateInventory");

		}

    }
}





